<?php

namespace App\Http\Middleware;

use App\Models\ShopUser;
use Closure;
use Dingo\Api\Facade\Route;
use App\Http\Controllers\Business\Shop\V1\Auth\ShopAuth;
use Illuminate\Support\Facades\Redis;

class ShopApiAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(!$request->get('token')) {
            echo json_encode(array(
                'status'=>'failed',
                'message'=>'认证失败，重新登录'
            ));
            exit;
        } else {
            $userid = ShopAuth::tokenGetUserId($request->get('token'));
            if($userid) {
                $profile = Route::currentRouteName();
                $shopid = $request->get('shopid');
                if($profile) {
                    if(!$shopid) {
                        echo json_encode(array(
                            'status'=>'failed',
                            'message'=>'未指定商家！'
                        ));
                        exit;
                    }
                    $auth = Redis::get('ShopAuth:'.$userid);
                    if($auth) {
                        $auth = json_decode($auth,true);
                        if(is_int(array_search($profile,$auth[$shopid]))) {
                            return $next($request);
                        } else {
                            echo json_encode(array(
                                'status'=>'failed',
                                'message'=>'您没有权限访问该模块！'
                            ));
                            exit;
                        }
                    } else {
                        $auth = ShopUser::where('id', $userid)->pluck('auth')->first();
                        if($auth) {
                            Redis::set('ShopAuth:'.$userid,$auth);
                            $auth = json_decode($auth,true);
                            if(is_int(array_search($profile,$auth[$shopid]))) {
                                return $next($request);
                            } else {
                                echo json_encode(array(
                                    'status'=>'failed',
                                    'message'=>'您没有权限访问该模块！'
                                ));
                                exit;
                            }
                        } else {
                            echo json_encode(array(
                                'status'=>'failed',
                                'message'=>'您没有权限访问该模块！'
                            ));
                            exit;
                        }
                    }
                } else {
                    return $next($request);
                }
            } else {
                echo json_encode(array(
                    'status'=>'failed',
                    'message'=>'认证失败，重新登录'
                ));
                exit;
            }
        }
    }
}
